package com.lxk.controller;

import com.lxk.pojo.Moment;
import com.lxk.service.MomentService;
import com.lxk.vo.MomentComShowModel;
import com.lxk.vo.MomentModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author JohnSoybean
 * @create 2020/4/3 16:34
 * @description
 */
@Controller
public class MomentController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MomentService ms;

    /**
     * 初始化待审核订单页面
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/checkMoment")
    public String toCheckMoment(HttpServletRequest req, Model model,
                           Integer pageIndex, Integer num, Integer totalPages,
                           Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = ms.getUncheckedTotalPages(num);
        }
        PageModel<Moment> momentInfo = ms.selUncheckedMomentPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", momentInfo);
        return "checkMoment";
    }

    /**
     * 初始化已通过订单页面
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/moment")
    public String toMoment(HttpServletRequest req, Model model,
                           Integer pageIndex, Integer num, Integer totalPages,
                           Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12    ;
        }
        if (null == totalPages) {
            totalPages = ms.getTotalPages(num);
        }
        PageModel<MomentModel> momentInfo = ms.selMomentPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", momentInfo);
        return "moment";
    }

    /**
     * 审核动态
     * @param momentId
     * @param isPassed
     * @return
     */
    @GetMapping("/moment/pass")
    @ResponseBody
    public Result<Integer> passMoment(Integer momentId, Integer isPassed) {
        ms.checkMoment(momentId, isPassed);
        return Result.success(1);
    }


    /**
     * 动态详情
     * @param momentId
     * @return
     */
    @GetMapping("/moment/detail")
    @ResponseBody
    public Result<Moment> getMomentdetail(Integer momentId) {
        Moment moment = ms.selMomentById(momentId);
        return Result.success(moment);
    }


    /**
     * 置顶动态
     * @param momentId
     * @return
     */
    @GetMapping("/moment/top")
    @ResponseBody
    public Result<Integer> shiftTopMoment(Integer momentId) {
        Moment moment = ms.selMomentById(momentId);
        Integer isTop = moment.getIsTop();
        if (isTop==0) {
            moment.setIsTop(1);
        }else {
            moment.setIsTop(0);
        }
        ms.updMoment(moment);
        return Result.success(1);
    }

    /**
     * 删除动态
     * @param momentId
     * @return
     */
    @GetMapping("/moment/delete")
    @ResponseBody
    public Result<Integer> deleteMoment(Integer momentId) {
        Moment moment = new Moment();
        moment.setMomentId(momentId);
        moment.setIsPassed(-1);
        ms.updMoment(moment);
        return Result.success(1);
    }


    /**
     * 获取动态评论的分页
     * @param request
     * @param response
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param momentId
     * @return
     */
    @GetMapping("/moment/comment")
    @ResponseBody
    public Result<PageModel<MomentComShowModel>> showAllGoodsCom(HttpServletRequest request, HttpServletResponse response,
                                                                 Integer pageIndex, Integer num, Integer totalPages, Integer momentId) {
        if(null==num) {
            num = 8;
        }
        if(null==pageIndex) {
            pageIndex = 1;
        }
        if(null==totalPages) {
            totalPages = ms.getComTotalPages(num, momentId);
        }
        PageModel<MomentComShowModel> model = ms.getComList(pageIndex, num, totalPages, momentId);
        return Result.success(model);
    }


    /**
     * 删除动态评论
     * @param momentComId
     * @return
     */
    @GetMapping("/moment/comment/delete")
    @ResponseBody
    public Result<Integer> deleteMomentComment(Integer momentComId) {
        ms.deleteMomentComment(momentComId);
        return Result.success(1);
    }

}
